package com.situ.skymallproductapi.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.model.Product;
import com.situ.model.search.ProductSearchBean;
import com.situ.service.ProductService;
import com.situ.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/api/v1/products", produces = "application/json;charset=utf-8")
public class ProductApi {
    public ProductService productService;

    @Autowired
    public void setProductService(ProductService productService) {
        this.productService = productService;
    }

    /*
      查询所有商品
      @return 商品分页列表
     */
    @GetMapping
    public ResponseEntity<JsonResult<?>> getLst(@RequestParam(defaultValue = "1") Integer current,
                                                @RequestParam(defaultValue = "9") Integer size,
                                                ProductSearchBean searchBean) {
        Page<Product> page = new Page<>(current, size);
        page = productService.getList(page, searchBean);
        if (page == null) {
            return ResponseEntity.ok(JsonResult.fail("暂无商品"));
        } else {
            return ResponseEntity.ok(JsonResult.success(page));
        }
    }

    /*
      根据id查询商品
      @param id 商品id
      @return 商品
     */
    @GetMapping("/id/{id}")
    public ResponseEntity<JsonResult<?>> getById(@PathVariable Integer id) {
        Product product = productService.getById(id);
        if (product == null) {
            return ResponseEntity.ok(JsonResult.fail("商品不存在"));
        } else {
            return ResponseEntity.ok(JsonResult.success(product));
        }
    }

    /*
      保存商品
      @param product 商品
      @return 是否保存成功
     */
    @PutMapping
    public ResponseEntity<JsonResult<?>> save(@RequestBody Product product) {
        boolean result = productService.save(product);
        if (result) {
            return ResponseEntity.ok(JsonResult.success("保存成功"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("保存失败"));
        }
    }

    /*
      更新商品
      @param product
      @return 是否更新成功
     */
    @PostMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody Product product) {
        boolean result = productService.updateById(product);
        if (result) {
            return ResponseEntity.ok(JsonResult.success("更新成功"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("更新失败"));
        }
    }

    /**
     * 设置秒杀商品库存
     * @param productId 商品id
     * @param delta 增加的库存数量
     */
    @PutMapping("/qty/{productId}/{delta}")
    public ResponseEntity<JsonResult<?>> updateQty(@PathVariable Integer productId,
                                                   @PathVariable int delta) {
        boolean success = productService.updateForIncrementQty(productId, delta);
        if (success) {
            return ResponseEntity.ok(JsonResult.success("更新成功"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("更新库存失败"));
        }
    }


    /*
      删除商品
      @param id 商品id
      @return 是否删除成功
     */
    @DeleteMapping("/id/{id}")
    public ResponseEntity<JsonResult<?>> deleteById(@PathVariable Integer id) {
        boolean result = productService.deleteById(id);
        if (result) {
            return ResponseEntity.ok(JsonResult.success("删除成功"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("删除失败"));
        }
    }

    /**
     * 切换商品的秒杀状态
     *
     * @param id 商品id
     * @return 是否切换成功
     */
    @PostMapping("/toggleSeckill/{id}")
    public ResponseEntity<JsonResult<?>> toggleSeckill(@PathVariable Integer id) {
        boolean result = productService.toggleSeckill(id);
        if (result) {
            return ResponseEntity.ok(JsonResult.success("切换成功"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("切换失败"));
        }
    }

    /**
     * 切换商品的上架状态
     *
     * @param id 商品id
     * @return 是否切换成功
     */
    @PostMapping("/toggleOnSale/{id}")
    public ResponseEntity<JsonResult<?>> toggleOnSale(@PathVariable Integer id) {
        boolean result = productService.toggleOnSale(id);
        if (result) {
            return ResponseEntity.ok(JsonResult.success("切换成功"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("切换失败"));
        }
    }

    /*
      根据分类id查询商品
      @param categoryId 分类id
      @return 商品列表
     */
    @GetMapping("/category/{categoryId}")
    public ResponseEntity<JsonResult<?>> getByCategoryId(@PathVariable Integer categoryId) {
        List<Product> products = productService.getByCategoryId(categoryId);
        if (products == null) {
            return ResponseEntity.ok(JsonResult.fail("该分类暂无商品"));
        } else {
            return ResponseEntity.ok(JsonResult.success(products));
        }
    }

    /**
     * 扣减库存
     * @param productId 商品id
     * @param delta 扣减数量
     * @return 是否扣减成功
     */
    @PostMapping("/decrementStock/id/{productId}/delta/{delta}")
    public ResponseEntity<JsonResult<?>> decrementStock(@PathVariable Integer productId,
                                                       @PathVariable int delta) {
        boolean result = productService.decrementStock(productId, delta);
        if (result) {
            return ResponseEntity.ok(JsonResult.success("扣减成功"));
        } else {
            return ResponseEntity.ok(JsonResult.fail("扣减失败"));
        }
    }
}
